package com.martin.lib_base.base

import android.content.pm.ActivityInfo
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.viewModels
import androidx.lifecycle.ViewModelProvider
import com.martin.lib_base.interfaces.ILoading
import com.martin.lib_base.ktx.loge
import com.martin.lib_base.model.LoadingModel

/**
 * @author：孟凡华
 * @date：2023/6/26
 * @desc：
 */
abstract class BaseActivity : ComponentActivity(), ILoading {

    //加载弹窗
    private val loadingModel: LoadingModel by lazy {
        LoadingModel(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        /*
            强制设置为竖屏
         */
        if (requestedOrientation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }
        beforeSetContent()
        initContent()
        initOnCreate(savedInstanceState)
    }

    /**
     * 设置布局之前
     */
    open fun beforeSetContent() {}

    abstract fun initContent()

    /**
     * 初始化Activity内容
     */
    open fun initOnCreate(savedInstanceState: Bundle?) {}

    override fun onResume() {
        super.onResume()
        if (showLife()) {
            loge("${getClassName()} : onResume")
        }
    }

    fun requireActivity() = this

    fun requireContext() = this

    /**
     * 展示加载框
     */
    override fun showLoading() {
        if (!isFinishing) {
            loadingModel.showLoading()
        }
    }

    /**
     * 隐藏加载弹窗
     */
    override fun dismissLoading() {
        loadingModel.dismissLoading()
    }

    override fun onPause() {
        super.onPause()
        if (showLife()) {
            loge("${getClassName()} : onPause")
        }
    }

    override fun onStop() {
        super.onStop()
        if (showLife()) {
            loge("${getClassName()} : onStop")
        }
    }

    override fun onDestroy() {
        dismissLoading()
        super.onDestroy()
        if (showLife()) {
            loge("${getClassName()} : onDestroy")
        }
    }

    private fun getClassName() = javaClass.simpleName

    /**
     * 展示生命周期
     */
    open fun showLife() = false
}